package com.zzyl.nursing.service.impl;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Random;

/**
 * 老人家属Service业务层处理
 *
 * @author laoYe
 * @date 2025-10-27
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WechatService wechatService;

    //注入时要使用rouyi框架的不要搞错
    @Autowired
    private TokenService tokenService;


    /*小程序端登录*/
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //1.获取openID,这个不可能变化是微信的唯一标识
        String openId = wechatService.getOpenid(dto.getCode());
        //2.获取用户的手机号码，这个有可能变化有可能网太差了换一张
        String phone = wechatService.getPhone(dto.getPhoneCode());
        //3.通过openId到数据库查询用户信息
        FamilyMember familyMemberInDB = lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        //为了做下面的两种状况写的对象
        FamilyMember familyMemberNew = new FamilyMember();
        //返回值对象
        LoginVo vo = new LoginVo();
        //4.判断用户是否存在
        if (familyMemberInDB != null) {
            //设置用户信息NickName
            vo.setNickName(familyMemberInDB.getName());
            //5.存在
            //手机号码已经变更就要更新手机号
            if (!familyMemberInDB.getPhone().equals(phone)) {
                //要明确更新的字段，不更新的字段不要更新
                //拿到id去更新手机号
                familyMemberNew.setId(familyMemberInDB.getId());
                familyMemberNew.setPhone(phone);
                //更新
                updateById(familyMemberNew);
            }
        } else {
            //6.不存在
            //创建用户设置openId
            familyMemberNew.setOpenId(openId);
            familyMemberNew.setPhone(phone);
            //设置昵称
            familyMemberNew.setName(getNickName(phone));
            vo.setNickName(getNickName(phone));
            save(familyMemberNew);
        }
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", familyMemberNew.getId());
        claims.put("phone", vo.getNickName());
        vo.setToken(tokenService.createToken(claims));
        return vo;
    }

    /*生成随机昵称*/
    private String getNickName(String phone) {
        Random r = new Random();
        int prefixIndex = r.nextInt(DEFAULT_NICKNAME_PREFIX.size());
        String prefix = DEFAULT_NICKNAME_PREFIX.get(prefixIndex);
        return prefix + phone.substring(phone.length() - 4);
    }

    //随机名称
    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );


    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return removeById(id) ? 1 : 0;
    }

}
